<?php
/**
 * Created by JetBrains PhpStorm.
 * User: ncduc
 * Date: 6/8/13
 * Time: 11:23 PM
 * To change this template use File | Settings | File Templates.
 */
include_once("libs/setup.php");
include_once("libs/helper.php");

session_start();

if(!check_user_session())
{
    header("Location: signin.php");
    return;
}

function load_application_items(Mysqli $mysqli,Smarty &$smarty)
{
    $userID = $_SESSION['ma_user'];
    $query = "select * from noi_dung_ung_tuyen";
    $result = $mysqli->query($query);
    if($result)
    {
        $applications = array();
        while ($row = $result->fetch_assoc())
        {
            $applyID = $row['Ma'];
            $query2 = "select * from ung_tuyen where `MaUser`={$userID} AND `NoiDungUngTuyen`={$applyID}";
            $result2 = $mysqli->query($query2);
                    $state=NULL;
            if($result2)
            {
                if($row2=$result2->fetch_assoc())
                {
                    $state= $row2['TinhTrang'];
                }
               
            }
            
            $applications[] = array('Ma' => $row['Ma'],
                'NoiDung'=>$row['NoiDung'],
                'TinhTrang'=>$state,
                'GhiChu'=>$row['GhiChu']);
            
        }
        $smarty->assign('items',$applications);
    }
    if($mysqli->error)
    {
        system_failed(NULL,$smarty);
        return false;
    }
    return true;
}

assign_home_header($smarty);
$mysqli = connect_database($smarty);
if(!load_application_items($mysqli, $smarty))
{
    return;
}
$smarty->display('home.tpl');
